package com.tsing.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tsing.common.result.PageResult;
import com.tsing.common.result.Result;
import com.tsing.pojo.vo.ArticleDetailsVO;
import com.tsing.pojo.vo.PostDetailsVO;
import com.tsing.service.ArticleService;
import com.tsing.service.impl.ArticleServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 吴阳基
 * @datetime 2023/12/8 11:25
 */
public class ArticleController {
    private final ArticleService articleService = new ArticleServiceImpl();

    public PageResult<ArticleDetailsVO> list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer channelId = (Integer) jsonObject.get("channelId");
        channelId = (channelId != null && channelId > 0) ? channelId : null;

        Long userId = jsonObject.getLong("userId");
        userId = (userId != null && userId > 0) ? userId : null;

        Integer page = (Integer) jsonObject.get("page");
        page = (page != null && page > 0) ? page : 1;

        Integer size = (Integer) jsonObject.get("size");
        size = (size != null && size > 0) ? size : 10;

        PageResult<ArticleDetailsVO> pageResult = articleService.list(channelId,page,size,userId);

        return pageResult;
    }

    //点赞之后，增加对应的点赞数
    public Result likeCountAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer postId = (Integer) jsonObject.get("postId");
        postId = (postId != null && postId > 0) ? postId : 1;

        Integer likeCount = (Integer) jsonObject.get("likeCount");
        likeCount = (likeCount != null && likeCount > 0) ? likeCount : 1;

        Integer list = articleService.likeCountAdd(postId,likeCount);

        return Result.success(list);
    }

    //取消点赞之后，删除对应的点赞数
    public Result likeCountDelete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer postId = (Integer) jsonObject.get("postId");
        postId = (postId != null && postId > 0) ? postId : 1;

        Integer likeCount = (Integer) jsonObject.get("likeCount");
        likeCount = (likeCount != null && likeCount > 0) ? likeCount : 1;

        Integer list = articleService.likeCountDelete(postId,likeCount);

        return Result.success(list);
    }

    //新增帖子（发布帖子）
    public Result add(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer userId = (Integer) jsonObject.get("userId");
        userId = (userId != null && userId > 0) ? userId : 1;

        String Copywriting = (String) jsonObject.get("Copywriting");
        Copywriting = (Copywriting != null && !Copywriting.isEmpty()) ? Copywriting : "暂无文案";

        String title = (String) jsonObject.get("title");
        title = (title != null && !title.isEmpty()) ? title : "暂无标题";

        String image = jsonObject.get("image").toString();
        image = (image != null && !image.isEmpty()) ? image : "";

        String createDate = (String) jsonObject.get("createDate");
        createDate = (createDate != null && !createDate.isEmpty()) ? createDate :
                LocalDateTime.now().toLocalDate().toString();

        String address = (String) jsonObject.get("address");
        address = (address != null && !address.isEmpty()) ? address : "湖南";

        Long channelId =  jsonObject.getLong("channelId");
        channelId = (channelId != null && channelId>0) ? channelId : 4;



        Integer list = articleService.add(userId,title,image,createDate,address,Copywriting,channelId);

        return Result.success(list);
    }

    //查看帖子详情
    public Result<List<PostDetailsVO>> postDetails(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer postId = jsonObject.getInteger("postId");
        postId = (postId != null && postId > 0) ? postId : null;

        Integer userId = jsonObject.getInteger("userId");
        userId = (userId != null && userId > 0) ? userId : null;


        List<PostDetailsVO> list = articleService.postDetails(postId,userId);

        return Result.success(list);
    }
}